package lightrain.db.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class ModelUtils {
	

	private static Map<String, String> tableNameCache = new ConcurrentHashMap<String,String>();
	
	public static String parseClassNameToTableName(Class<?> clasz){
		String tableName = tableNameCache.get(clasz.getName());
		if(tableName != null){
			return tableName;
		}
		String simpleName = clasz.getSimpleName();
		StringBuilder str = new StringBuilder();
		char[] array = simpleName.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if(Character.isUpperCase(array[i]) && i > 0){
				str.append("_"); 
			}
			str.append(array[i]);
		}
		tableName = str.toString().toLowerCase();
		tableNameCache.put(clasz.getName(), tableName);
		return tableName;
	}
	
	public static  <T> T newInstance(Class<T> clasz){
		try {
			return clasz.newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
